import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/home_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/web_view_page.dart';
import 'package:FineArt/page/imageview/RecommendImagePreview.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/page/imageview/imageview.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class RecommendGroupItem extends StatelessWidget {
  final dynamic group;
  final String type;

  List<RecommendResource> resources = new List();

  RecommendGroupItem({Key key, this.group, this.type}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin: EdgeInsets.symmetric(vertical: 5, horizontal: 16),
      width: double.infinity,
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(
          this.group.date,
          style: TextStyle(
              fontSize: setSp(12),
              fontWeight: FontWeight.normal,
              color: text_black),
        ),
        GridView(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, //横轴三个子widget
            // childAspectRatio: 1.0 ,//宽高比为1时，子widget
            crossAxisSpacing: getPortraitSWidth(3),
            mainAxisSpacing: getPortraitSHeight(3),
          ),
          //-- children: getImageList(8, url0),
          children: getImageList(context),
          //此属性设置后gridview才可以在listview嵌套
          shrinkWrap: true,
          // 处理GridView中滑动父级Listview无法滑动
          //也可这样设置 physics: NeverScrollableScrollPhysics()
          primary: false,
        )
      ]),

      // Here i can use size.width but use double.infinity because both work as a same
    );
  }

  List<Widget> getImageList(BuildContext context) {
    List<Widget> list = List();
    resources.clear();
    for (int i = 0; i < this.group.items.length; i++) {
      String url = this.group.items[i].thumburl;
      String avatar = this.group.items[i].portrait;
      String info = this.group.items[i].title.isNotEmpty
          ? (this.group.items[i].title + "/" + this.group.items[i].nickname)
          : this.group.items[i].nickname;
      list.add(buildImageItem(url, avatar, info,context));

      RecommendResource resource = new RecommendResource(resid:this.group.items[i].resid,sdurl: this.group.items[i].sdurl,hdurl: this.group.items[i].hdurl,type: this.group.items[i].type);
      resources.add(resource);
    }

    return list;
  }

  Widget buildImageItem(String url, String avatar, String info, BuildContext context) {
    Image avartImg;
    if (avatar == null) {
      avartImg = Image.asset(
        "assets/images/icon_header_default.png",
        width: getPortraitSWidth(21),
        height: getPortraitSHeight(21),
      );
    } else {
      avartImg = Image(
          width: getPortraitSWidth(21),
          height: getPortraitSHeight(21),
          image: CachedNetworkImageProvider(
            avatar,
          ));
    }
    Stack stack = new Stack(
      alignment: Alignment.center,
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(radius(20)),
          child: InkWell(
            onTap: () {
              // ToastUtils.toast(url);
              RecommendImagePreview.JumpToPreview(0, 0, resources, context);
            },
            child: Image(
                width: getPortraitSWidth(121),
                height: getPortraitSHeight(165),
                image: CachedNetworkImageProvider(
                  url,
                )),
          ),
        ),
        Positioned(
            bottom: getPortraitSHeight(5),
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(
                  vertical: 2, horizontal: getPortraitSWidth(8)),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(radius(20)),
                  color: Color.fromARGB(100, 0, 0, 0)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  avartImg,
                  SizedBox(
                    width: getPortraitSWidth(0),
                  ),
                  Text(
                    info,
                    style: TextStyle(color: Colors.white, fontSize: setSp(10)),
                  )
                ],
              ),
            ))
      ],
    );

    return stack;
  }
}
